import { Component, OnInit } from '@angular/core';
import { Router,ActivatedRoute,NavigationExtras } from '@angular/router';
import {HttpClient,HttpHeaders} from "@angular/common/http";
import { ToastController } from '@ionic/angular';
import {environment} from 'src/environments/environment';
import { EncryptService } from '@services/encrypt.service';
import { AuthTokenService } from '@services/auth-token.service';
import { CommonResponse,VerificationType,ToastColor,CommonService } from '@services/common-service.service'
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';
import { DefaultLanguageService } from '@services/default-language.service';

@Component({
  selector: 'app-input-mobile',
  templateUrl: './input-mobile.page.html',
  styleUrls: ['./input-mobile.page.scss'],
})
export class InputMobilePage implements OnInit {

  public appUser:any = {
    mobileNumber:'',
    countryCode:'86'
  }

  public loading:boolean = false;

  public i18n:any;
  
  constructor(private router: Router,
    private route: ActivatedRoute,
    public http:HttpClient,
    private toastController: ToastController,
    private encryptService:EncryptService,
    private authTokenService:AuthTokenService,
    private commonService:CommonService,
    private defaultLanguageService:DefaultLanguageService,
    private translate: TranslateService) {
      this.i18n = this.commonService.initTranslate(['auth.reset-pwd.input-mobile']); 
    }

  public validateForm:FormGroup;
  ngOnInit() {
    this.validateForm = new FormGroup({
      'mobileNumber': new FormControl('', [
        Validators.required
      ]),
      'countryCode': new FormControl(this.appUser.countryCode, [
        Validators.required
      ])
    });
  }

  get mobileNumber() { return this.validateForm.get('mobileNumber'); }

  signIn(){
    this.router.navigate(['/auth/signin-password']);
  }

  toInputVerificationCode(){
    if(this.validateForm.invalid){
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
      }
      return false;
    }
    this.checkMobileNumber();
  }

  checkMobileNumber(){
    this.loading = true;
    var requestJson = {
      'mobileNumber':this.validateForm.value.mobileNumber
    }
    var api = "/scm/app/checkAppUserExistsByMobile.do";
    this.http.post(api,requestJson).subscribe((response:CommonResponse) => {
      if(response.isSuccess){
        if(response.responseBody.isExists){
          let navigationExtras: NavigationExtras = {
            queryParams: {
              'appUserId':response.responseBody.appUserId
            }
          };
          this.router.navigate(['/auth/reset-pwd/input-verification-code'],navigationExtras);
        }else{
          this.commonService.commonToast(ToastColor.dark,this.i18n.mobileNumber_not_exists);
        }
      }
      this.loading = false;
    },(errorResponse)=>{
      this.commonService.errorRespCommonToast(errorResponse,this.i18n);
      this.loading = false;
    });
  }
}
